#include "vertex_types.fxh"
#include "Samplers.fxh"
#include "Functions.fxh"

float4 g_FogColor = (1.0, 0.0, 0.0, 1.0);
float g_StartLinearFog =  100;
float g_EndLinearFog =  200;
float g_atMax =  175;

float CalcAttenuation(float Depth, float StartFog, float EndFog)
{
	return  saturate((EndFog - Depth) / (EndFog - StartFog));
}

float4 CalcLinearFog(float Depth, float StartFog, float EndFog, float4 FogColor)
{
	if(Depth<g_atMax){
		return float4(FogColor.xyz, FogColor.a*(1.0-CalcAttenuation(Depth, StartFog, EndFog)));	
	}
	else{
		return float4(FogColor.xyz, FogColor.a*(1.0-CalcAttenuation(g_atMax, StartFog, EndFog)));	
	}
}

float4 RenderQuadFogPS(in float2 UV : TEXCOORD0) : COLOR
{
	float4 l_DepthColor=tex2D(S0LinearSampler, UV);
	//float3 l_ViewPosition=GetPositionFromZDepthViewInViewCoordinates(l_DepthColor, UV, g_InverseProjectionMatrix);
	float3 l_WorldPosition=GetPositionFromZDepthView(l_DepthColor, UV, g_InverseViewMatrix, g_InverseProjectionMatrix);
	float l_Depth=length(g_CameraPosition-l_WorldPosition);
	//return CalcExp2Fog(l_Depth, g_Exp2DensityFog, g_FogColor);
	//return CalcExpFog(l_Depth, g_ExpDensityFog, g_FogColor);
	return CalcLinearFog(l_Depth, g_StartLinearFog, g_EndLinearFog, g_FogColor);
}

technique DrawQuadFogTechnique
	{
	pass p0
	{
		AlphaBlendEnable = true;
		BlendOp=Add;
		SrcBlend=SrcAlpha;
		DestBlend=InvSrcAlpha;
		PixelShader=compile ps_3_0 RenderQuadFogPS();
	}
}